package com.example.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.user.constants.AuthUserConstant;
import com.example.user.dao.AuthUserDao;
import com.example.user.feign.AdminFeign;
import com.example.user.feign.AuthUserFeign;
import com.example.user.service.AuthUserService;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUserRealname;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 身份验证用户服务
 *
 */
@Service
@Slf4j
public class AuthUserServiceImpl implements AuthUserService {
    @Autowired
    AuthUserDao authUserDao;
    @Autowired
    private AdminFeign adminFeign;
    @Autowired
    private AuthUserFeign authUserFeign;
    //
    /**
     * 得到所有用户
     *
     * @param pageNum  当前页数
     * @param pageSize 每页数量
     * @return {@code List<ApUserRealname>}
     */
    @Override
    public List<ApUserRealname> getAllUser(Integer pageNum, Integer pageSize) {
        Page<ApUserRealname> page = new Page<>(pageNum, pageSize);
        IPage<ApUserRealname> selectPage = authUserDao.selectPage(page, null);
        return selectPage.getRecords();
    }
    
    
    /**
     * 审核接口
     *
     * @param authDto auth dto
     * @return {@code ResponseResult}
     */
    @Override
    public ResponseResult auditing(AuthDto authDto) {
        System.out.println(authDto);
        //校验前台参数
        if (authDto.getStatus() != AuthUserConstant.WAIT_AUTH && authDto.getStatus() != AuthUserConstant.SUC_AUTH && authDto.getStatus() != AuthUserConstant.FAIL_AUTH) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = new ApUserRealname();
        BeanUtils.copyProperties(authDto, apUserRealname);
        apUserRealname.setUpdatedTime(new Date());
        int i = authUserDao.updateById(apUserRealname);
        if (i == 1) {
            //修改成功  调用接口  创建用户
            //修改成功
            Integer id = authDto.getId();
            //获取用户ID
            Integer userId = authUserDao.selectById(id).getUserId();
            //查询用户信息，接口调用
            ResponseResult responseResult = adminFeign.getAdUserById(userId);
            log.info("===>data:{", responseResult.getData());
            //linkedHashmap转成对象（先转成json，json串转成对象）
            String adUserStr = JSONObject.toJSONString(responseResult.getData());
            AdUser adUser = JSONObject.parseObject(adUserStr, AdUser.class);
            if (adUser != null) {
                log.info("=====z=>adUser:{", adUser);
                //调用wemedia添加用户接口
                WmUserDto wmUserDto = new WmUserDto();
                BeanUtils.copyProperties(adUser, wmUserDto);
                wmUserDto.setApUserId(userId);
                authUserFeign.addWmUser(wmUserDto);
                
                
                return ResponseResult.okResult("成功");
            }
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        return null;
    }
    
    
    @Override
    public ResponseResult saveImg(String imgPath) {
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(1);
        apUserRealname.setBackImage(imgPath);
        apUserRealname.setUpdatedTime(new Date());
        int i = authUserDao.updateById(apUserRealname);
        if (i == 1) {
            return ResponseResult.okResult("修改成功");
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        
    }
    
    /**
     * 得到用户通过id
     *
     * @param id id
     * @return {@code ApUserRealname}
     */
    @Override
    public ApUserRealname getUserById(Integer id) {
        return authUserDao.selectById(id);
    }
    
    @Override
    public Integer update(ApUserRealname apUserRealname) {
        apUserRealname.setUpdatedTime(new Date());
        return authUserDao.updateById(apUserRealname);
    }
    
}
